<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/28 0028
 * Time: 14:12
 */

namespace Admin\Controller;


class ScreenAdController extends AdminController
{
    public function index()
    {
        /* 查询条件初始化 */
        $map = array();
        if(isset($_GET['ad_type']) && $_GET['ad_type'] > 0){
            $map['ad_type']   =   I('ad_type',1);
        }
        if(isset($_GET['order_no'])){
            $map['order_no']    =   array('like', '%'.(string)I('order_no').'%');
        }

        $list = $this->lists('AdOrder', $map,'status asc,create_at desc');
        $ad_order_list = M('AdOrderList')->where(['order_no' => array('in',array_unique(array_column($list,'order_no')))])->select();

        $member_list = M('Usermember')->where(['id' => array('in',array_unique(array_column($list,'mid')))])->select();
        $shop_list = M('Shop')->where(['id' => array('in',array_unique(array_column($ad_order_list,'shop_id')))])->field('id,title')->select();
        $store_list = M('ShopStore')->where(['id' => array('in',array_unique(array_column($ad_order_list,'store_id')))])->field('id,title')->select();
        $screen_list = M('ShopStoreScreen')->where(['id' => array('in',array_unique(array_column($ad_order_list,'screen_id')))])->field('id,title,equipment')->select();

        //列表处理
        foreach ($list as &$item) {
            list($item['list'],$item['member']) = [[],[]];
            foreach ($ad_order_list as $order_list) if($item['order_no'] === $order_list['order_no']) {
                list($order_list['shop'],$order_list['store'],$order_list['screen']) = [[],[],[]];
                foreach ($shop_list as $shop) if($order_list['shop_id'] === $shop['id']) {
                    $order_list['shop'] = $shop;
                }
                foreach ($store_list as $store) if($order_list['store_id'] === $store['id']) {
                    $order_list['store'] = $store;
                }
                foreach ($screen_list as $screen) if($order_list['screen_id'] === $screen['id']) {
                    $order_list['screen'] = $screen;
                }
                $item['list'][] = $order_list;
            }
            foreach ($member_list as $member) if($item['mid'] === $member['id']) {
                $item['member'] = $member;
            }
        }
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->assign('ad_type',I('get.ad_type',0));
        $this->assign('list', $list);
        $this->meta_title = '广告订单列表';
        $this->display();
    }
    public function resume()
    {
        $id=I('get.id');
        $data['status']=1;
        $res=M('AdOrder')->where(array('id'=>$id))->save($data);
        if($res){
            $this->success('操作成功',Cookie('__forward__'));
        } else {
            $this->error('操作失败！');
        }
    }
}